<template>
  <div class="donation-records-container">
    <h2 class="page-title">捐赠记录</h2>
    <div class="records-content">
      <el-card class="records-card">
        <el-table
          v-loading="loading"
          :data="records"
          style="width: 100%"
          :header-cell-style="{ background: '#f5f7fa', fontWeight: '600' }"
          :row-class-name="tableRowClassName"
        >
          <el-table-column prop="projectId" label="项目编号" width="100" align="center" />
          <el-table-column prop="donorName" label="捐赠人" min-width="120" />
          <el-table-column prop="amount" label="捐赠金额" width="120" align="right">
            <template #default="{ row }">
              <span class="amount">¥{{ row.amount.toFixed(2) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="message" label="留言" min-width="200" show-overflow-tooltip />
          <el-table-column prop="createTime" label="捐赠时间" width="180" align="center">
            <template #default="{ row }">
              <span class="time-text">{{ formatDate(row.createTime) }}</span>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 30, 50]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            background
          />
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '@/utils/request'

// 数据相关
const records = ref([])
const loading = ref(false)
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)

// 从localStorage获取用户信息
const userInfo = JSON.parse(localStorage.getItem('userInfo'))

// 获取捐赠记录
const getRecords = async () => {
  loading.value = true
  try {
    const res = await request.get(`/donation/records/user/${userInfo.id}`, {
      params: {
        pageNum: currentPage.value,
        pageSize: pageSize.value
      }
    })
    records.value = res.data.list
    total.value = res.data.total
  } catch (error) {
    console.error('获取捐赠记录失败:', error)
    ElMessage.error('获取捐赠记录失败')
  } finally {
    loading.value = false
  }
}

// 格式化日期
const formatDate = (date) => {
  return new Date(date).toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 表格行样式
const tableRowClassName = ({ rowIndex }) => {
  return rowIndex % 2 === 0 ? 'even-row' : 'odd-row'
}

// 分页处理
const handleSizeChange = (size) => {
  pageSize.value = size
  getRecords()
}

const handleCurrentChange = (page) => {
  currentPage.value = page
  getRecords()
}

// 页面加载时获取数据
onMounted(() => {
  getRecords()
})
</script>

<style scoped lang="scss">
.donation-records-container {
  padding: 24px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
}

.page-title {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 32px;
  color: #2c3e50;
  border-bottom: 2px solid #e4e7ed;
  padding-bottom: 16px;
  position: relative;

  &::after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 100px;
    height: 2px;
    background: #409eff;
  }
}

.records-content {
  .records-card {
    border-radius: 12px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
    }
  }
}

:deep(.even-row) {
  background-color: #fafafa;
}

:deep(.odd-row) {
  background-color: #ffffff;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #f5f7fa !important;
  font-size: 15px;
  padding: 16px 0;
}

:deep(.el-table td) {
  padding: 14px 0;
}

.amount {
  color: #67c23a;
  font-weight: 600;
  font-size: 15px;
  background: rgba(103, 194, 58, 0.1);
  padding: 4px 8px;
  border-radius: 4px;
}

.time-text {
  color: #606266;
  font-size: 14px;
}

.pagination-container {
  margin-top: 24px;
  display: flex;
  justify-content: center;
  padding: 16px 0;
}

// 响应式布局
@media (max-width: 768px) {
  .donation-records-container {
    padding: 16px;
  }

  .page-title {
    font-size: 24px;
    margin-bottom: 24px;
  }

  :deep(.el-table th) {
    padding: 12px 0;
    font-size: 14px;
  }

  :deep(.el-table td) {
    padding: 10px 0;
  }
}
</style>